package com.masterclient.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.eoner.commonbean.popup.CommonPopUpBean;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.masterclient.app.modules.firstpager.fragments.adapters.NewcommerProductAdapter;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/7/9.
 */
public class NewcomerProductDialog extends Dialog {

    @BindView(R.id.dialog_newcomer_product_img)
    ImageView dialogNewcomerProductImg;
    @BindView(R.id.dialog_newcomer_product_recycler)
    RecyclerView dialogNewcomerProductRecycler;
    @BindView(R.id.dialog_newcomer_product_left)
    ImageView dialogNewcomerProductLeft;
    @BindView(R.id.dialog_newcomer_product_right)
    ImageView dialogNewcomerProductRight;

    private Context context;
    private CommonPopUpBean imageDialogBean;
    private FinishPage finishPage;

    public interface FinishPage{
        void onFinishPage();
    }

    public NewcomerProductDialog(@NonNull Context context, CommonPopUpBean imageDialogBean, FinishPage finishPage) {
        super(context);
        this.context = context;
        this.imageDialogBean = imageDialogBean;
        this.finishPage = finishPage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_newcomer_product);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);

        ViewTransformUtil.glideImageView(context, imageDialogBean.getImage(), dialogNewcomerProductImg
                ,R.color.transparent);


        if (imageDialogBean.getButtons()!=null && imageDialogBean.getButtons().size() > 0) {
            if (imageDialogBean.getButtons().size() == 1) {
                BannerNewItemBean buttonBean = imageDialogBean.getButtons().get(0);
                dialogNewcomerProductRight.setVisibility(View.GONE);
                ViewTransformUtil.glideImageView(context, buttonBean.getImage(), dialogNewcomerProductLeft
                        , R.color.transparent);
            } else {
                BannerNewItemBean buttonBean1 = imageDialogBean.getButtons().get(0);
                BannerNewItemBean buttonBean2 = imageDialogBean.getButtons().get(1);

                dialogNewcomerProductRight.setVisibility(View.VISIBLE);
                ViewTransformUtil.glideImageView(context, buttonBean1.getImage(), dialogNewcomerProductRight
                        , R.color.transparent);

                ViewTransformUtil.glideImageView(context, buttonBean2.getImage(), dialogNewcomerProductLeft
                        , R.color.transparent);
            }
        }


        dialogNewcomerProductRecycler.setLayoutManager(new LinearLayoutManager(context));
        NewcommerProductAdapter newcomerAdapter = new NewcommerProductAdapter();
        dialogNewcomerProductRecycler.setAdapter(newcomerAdapter);
        newcomerAdapter.bindToRecyclerView(dialogNewcomerProductRecycler);
        newcomerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                        .withString("source_position", position+"")
                        .withString("id",newcomerAdapter.getData().get(position).getProduct_id()).navigation();
            }
        });

        newcomerAdapter.setNewData(imageDialogBean.getProducts());


    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        windowparams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        window.setAttributes(windowparams);
    }

    @OnClick({R.id.dialog_newcomer_product_left, R.id.dialog_newcomer_product_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_newcomer_product_img:
                CommonUtil.goNewAnyWhere(context,imageDialogBean.getTarget_platform(),imageDialogBean.getTarget_message(),imageDialogBean.getTarget(),imageDialogBean.getTarget_id(),"","","");
                break;
            case R.id.dialog_newcomer_product_left:
                dismiss();
                BannerNewItemBean itemBean1 = imageDialogBean.getButtons().get(0);
                intentPage(itemBean1.getTarget_platform()
                        ,itemBean1.getTarget_message()
                        ,itemBean1.getTarget()
                        ,itemBean1.getTarget_id());
                break;
            case R.id.dialog_newcomer_product_right:
                BannerNewItemBean itemBean2 = imageDialogBean.getButtons().get(1);
                intentPage(itemBean2.getTarget_platform()
                        ,itemBean2.getTarget_message()
                        ,itemBean2.getTarget()
                        ,itemBean2.getTarget_id());
                dismiss();
                break;
        }
    }

    private void intentPage(List<String>targetPlatform,String message, String target, String targetId){

        if ("last_page".equals(target)){
            finishPage.onFinishPage();
        }else if ("close".equals(target)){

        }else {
            CommonUtil.goNewAnyWhere(context,targetPlatform,message,target,targetId,"","","");
        }
    }
}
